package a2022.a20220922;

/**
 * @author lenovo
 * @date 2022/9/22
 */
public class a {
    public static void main(String[] args) {
        System.out.println(canFormArray2(new int[]{91, 4, 64, 78}, new int[][]{
                {78},
                {4, 64},
                {91}}));
//        System.out.println(canFormArray(new int[]{37,69,3,74,46}, new int[][]{
//                {37,69,3,74,46}}));
//        System.out.println(canFormArray(new int[]{1,3,5,7}, new int[][]{
//                {2,4,6,8}}));
    }


    public static boolean canFormArray(int[] arr, int[][] pieces) {
        for (int i = 0; i < pieces.length; i++) {
            if (pieces[i].length > 1) {
                int n = pieces[i].length;
                int m =0;
                for (int i1 = 0; i1 < arr.length; i1++) {
                    if (arr[i1] == pieces[i][0]){
                        if ((i1+n)>arr.length) return false;
                        for (int b =1;b<n;b++) {
                            i1++;
                            if (arr[i1] != pieces[i][b]) return false;
                        }
                    }else{
                        m++;
                    }
                }
                if (m >= arr.length) return false;
            } else {
                int a = 1;
                for (int i1 = 0; i1 < arr.length; i1++) {
                    if (arr[i1] == pieces[i][0]) {
                        a = 0;
                        break;
                    }
                }
                if (a > 0) return false;
            }
        }
        return true;
    }

    public static boolean canFormArray2(int[] arr, int[][] pieces) {
        int n = arr.length, m = pieces.length;
        int[] hash = new int[110];
        for (int i = 0; i < m; i++) hash[pieces[i][0]] = i;
        for (int i = 0; i < n; ) {
            int[] cur = pieces[hash[arr[i]]];
            int len = cur.length, idx = 0;
            while (idx < len && cur[idx] == arr[i + idx]) idx++;
            if (idx == len) i += len;
            else return false;
        }
        return true;
    }

}
